#ifndef __FRAME_H_
#define __FRAME_H_

#include <list.h>
#include <threads/synch.h>
//#include <threads/thread.h>
#include <userprog/pagedir.h>
#include <threads/palloc.h>
#include <threads/vaddr.h>

struct FTE
{
    struct list_elem elem;
    //struct lock lock;
    uintptr_t frame_number;
    struct thread *owner_thread;
    uintptr_t owner_page_number;
    uint8_t pin_num;
};

struct list frame_table;
struct lock frame_table_lock;

/*初始化函数*/
void frame_init(void);

/*将一个帧设定为已被使用*/
bool
regist_frame(uintptr_t frame_n,uintptr_t page_n);

/*将一个帧释放*/
bool
unregist_frame(uintptr_t frame_n);

/*找到指定的帧*/
struct FTE *
find_frame(uintptr_t frame_n);

/*申请一个帧*/
void *falloc_get_frame(bool zero);

/*释放一个帧*/
bool falloc_free(void *);

/*从当前的帧表中挑选一个帧用于替换*/
struct FTE *select_frame();

/* pin住当前帧，使其无法被换出内存*/
bool pin_frame(uintptr_t frame_n);

/*unpin*/
bool unpin_frame(uintptr_t frame_n);

inline bool force_pin(void *vaddr);

bool unpin_page(uintptr_t page_n);

void frame_unregist_all(struct thread *t);

#endif
